<template>
  <div class="dashboard">
    <!-- 美化后的公告区域 -->
    <div class="announcement-card">
      <div class="announcement-header">
        <div class="announcement-icon">
          <i class="icon-megaphone">📢</i>
        </div>
        <div class="header-content">
          <span class="announcement-title">{{ greeting }}</span>
          <span class="announcement-subtitle">欢迎使用星屹远管理系统</span>
        </div>
        <div class="weather-info" v-if="showWeather">
          <span class="weather-icon">☀️</span>
          <span class="weather-temp">今天也要开心哦</span>
        </div>
      </div>

      <div class="announcement-content">
        <div class="time-display">
          <div class="time-main">
            <div class="current-time">{{ currentTime }}</div>
            <div class="time-separator"></div>
            <div class="date-week">
              <div class="current-date">{{ currentDate }}</div>
              <div class="current-week">{{ currentWeek }}</div>
            </div>
          </div>
          <div class="time-progress">
            <div class="progress-bar">
              <div class="progress-fill" :style="{ width: timeProgress + '%' }"></div>
            </div>
            <div class="progress-label">今日进度 {{ timeProgress }}%</div>
          </div>
        </div>

        <div class="system-status">
          <div class="status-item">
            <div class="status-icon success">✓</div>
            <div class="status-info">
              <div class="status-title">系统状态</div>
              <div class="status-desc">运行正常</div>
            </div>
          </div>
          <div class="status-item" v-for="(item,i) in userReport" :key="i">
            <div class="status-icon warning">{{ item.icon }}</div>
            <div class="status-info">
              <div class="status-title">{{ item.itemName }}</div>
              <div class="status-desc">{{ item.amount }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 数据统计卡片保持不变 -->
    <div v-hasPermi="['business:payRecord:report']" class="stats-section">
      <div class="stats-cards">
        <el-row :gutter="20">
          <el-col v-for="(item, index) in report" :key="index" :xs="24" :sm="12" :md="8" :lg="4" :xl="4">
            <div class="stat-card" :class="`stat-card-${index + 1}`">
              <div class="stat-content">
                <div class="stat-main">
                  <div class="stat-value">{{ item.amount }}</div>
                  <div class="stat-label">{{ item.itemName }}</div>
                </div>
                <div class="stat-icon">
                  <div class="icon-wrapper">
                    {{ item.icon }}
                  </div>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>

    <!-- 流量趋势图表 -->
    <div class="chart-section">
      <div class="chart-card">
        <div class="chart-header">
          <div class="chart-title">
            <div class="main-title">收款数据</div>
            <div class="sub-title">30天收款数据</div>
          </div>
        </div>
        <div class="chart-container">
          <!-- 这里放置面积图 -->
          <div class="area-chart-placeholder" ref="commandstats"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="Index" lang="ts">
import {ref, onMounted, onBeforeUnmount, computed} from 'vue';
import {getReportApi, getUserReportApi, echartsReportApi} from '@/api/pay/topUp';

// 报表数据
const report = ref<any>({});
const userReport = ref<any>({});
const getReport = async () => {
  const res = await getReportApi();
  report.value = res.data;
};
const getUserReport = async () => {
  const res = await getUserReportApi();
  userReport.value = res.data;
};

//图表
import * as echarts from 'echarts';

const commandstats = ref("");
const dataList = ref([]);
const xAxisList = ref([]);
const getList = async (userId) => {
  const res = await echartsReportApi();
  dataList.value = res.data.data;
  xAxisList.value = res.data.xAxis;
  initEcharts();
};
//初始化图
const initEcharts = () => {
  const commandstatsIntance = echarts.init(commandstats.value, 'macarons');
  commandstatsIntance.setOption({
    xAxis: {
      type: 'category',
      data: xAxisList.value,
      // boundaryGap: false
    },
    yAxis: {
      type: 'value',
      // boundaryGap: false
    },
    series: [
      {
        data: dataList.value,
        type: 'line',
        label: {
          show: true,
          position: 'top'
        }
      }
    ],
    grid: {
      top: '2%', //默认10%
      left: '2%', //默认10%
      right: '2%', //默认10%
      bottom: '1%', //默认60
      containLabel: true
      //grid区域是否包含坐标轴的刻度标签
    }
  });
  window.addEventListener('resize', () => {
    commandstatsIntance.resize();
  });
}

// 顶部计时
const greeting = ref('');
const currentDate = ref('');
const currentTime = ref('');
const currentWeek = ref('');
const showWeather = ref(true);
let timer: number | null = null;

// 计算今日进度百分比
const timeProgress = computed(() => {
  const now = new Date();
  const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  const endOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
  const total = endOfDay.getTime() - startOfDay.getTime();
  const passed = now.getTime() - startOfDay.getTime();
  return Math.round((passed / total) * 100);
});

// 获取问候语
const getGreeting = () => {
  const hour = new Date().getHours();
  if (hour >= 5 && hour < 9) {
    return '早上好，今天也是元气满满的一填！';
  } else if (hour >= 9 && hour < 12) {
    return '上午好，撸起袖子加油干吧！';
  } else if (hour >= 12 && hour < 14) {
    return '中午好，再忙也要记得吃午餐哦！';
  } else if (hour >= 14 && hour < 18) {
    return '下午好，来杯下午茶放松一下吧！';
  } else if (hour >= 18 && hour < 23) {
    return '晚上好，不要太劳累哦！';
  } else {
    return '夜深了，注意早点休息哦！';
  }
};

// 格式化日期时间
const formatTime = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hour = String(now.getHours()).padStart(2, '0');
  const minute = String(now.getMinutes()).padStart(2, '0');
  const second = String(now.getSeconds()).padStart(2, '0');

  // 星期处理
  const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  const weekDay = weekDays[now.getDay()];

  currentDate.value = `${year}年${month}月${day}日`;
  currentTime.value = `${hour}:${minute}:${second}`;
  currentWeek.value = weekDay;
  greeting.value = getGreeting();
};

// 初始化时间显示
const initTimeDisplay = () => {
  formatTime();
  timer = setInterval(formatTime, 1000);
};

// 清理定时器
const clearTimer = () => {
  if (timer) {
    clearInterval(timer);
    timer = null;
  }
};

onMounted(() => {
  getReport();
  getUserReport();
  getList();
  initTimeDisplay();
});

onBeforeUnmount(() => {
  clearTimer();
});
</script>

<style scoped lang="scss">
.dashboard {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e3e9f7 100%);
  min-height: 100vh;
}

// 美化公告卡片样式
.announcement-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 20px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 10px 30px rgba(102, 126, 234, 0.2),
  0 4px 12px rgba(0, 0, 0, 0.1);
  color: white;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: -50%;
    right: -50%;
    width: 100%;
    height: 200%;
    background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
    transform: rotate(45deg);
    animation: shimmer 3s ease-in-out infinite;
  }
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%) translateY(-100%) rotate(45deg);
  }
  100% {
    transform: translateX(100%) translateY(100%) rotate(45deg);
  }
}

.announcement-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  position: relative;
  z-index: 2;

  .announcement-icon {
    width: 48px;
    height: 48px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 16px;
    backdrop-filter: blur(10px);

    .icon-megaphone {
      font-size: 24px;
      filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
    }
  }

  .header-content {
    flex: 1;

    .announcement-title {
      font-size: 20px;
      font-weight: 700;
      display: block;
      margin-bottom: 4px;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .announcement-subtitle {
      font-size: 14px;
      opacity: 0.9;
      font-weight: 400;
    }
  }

  .weather-info {
    background: rgba(255, 255, 255, 0.2);
    padding: 8px 16px;
    border-radius: 20px;
    backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    gap: 8px;

    .weather-icon {
      font-size: 18px;
    }

    .weather-temp {
      font-weight: 600;
      font-size: 16px;
    }
  }
}

.announcement-content {
  position: relative;
  z-index: 2;

  .time-display {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 16px;
    padding: 24px;
    margin-bottom: 20px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);

    .time-main {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 16px;

      .current-time {
        font-size: 48px;
        font-weight: 800;
        font-family: 'Courier New', monospace;
        letter-spacing: 2px;
        text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        background: linear-gradient(45deg, #fff, #e3f2fd);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
      }

      .time-separator {
        width: 2px;
        height: 60px;
        background: linear-gradient(180deg, transparent, rgba(255, 255, 255, 0.5), transparent);
        margin: 0 20px;
      }

      .date-week {
        text-align: right;

        .current-date {
          font-size: 18px;
          font-weight: 600;
          margin-bottom: 4px;
        }

        .current-week {
          font-size: 16px;
          opacity: 0.9;
        }
      }
    }

    .time-progress {
      .progress-bar {
        width: 100%;
        height: 6px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 3px;
        overflow: hidden;
        margin-bottom: 8px;

        .progress-fill {
          height: 100%;
          background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
          border-radius: 3px;
          transition: width 0.3s ease;
          box-shadow: 0 0 20px rgba(79, 172, 254, 0.5);
        }
      }

      .progress-label {
        font-size: 12px;
        opacity: 0.8;
        text-align: center;
      }
    }
  }

  .system-status {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
    gap: 16px;

    .status-item {
      display: flex;
      align-items: center;
      background: rgba(255, 255, 255, 0.1);
      padding: 16px;
      border-radius: 12px;
      backdrop-filter: blur(10px);
      border: 1px solid rgba(255, 255, 255, 0.05);
      transition: transform 0.2s ease;

      &:hover {
        transform: translateY(-2px);
        background: rgba(255, 255, 255, 0.15);
      }

      .status-icon {
        width: 40px;
        height: 40px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 12px;
        font-weight: bold;

        &.success {
          background: linear-gradient(135deg, #4CAF50, #45a049);
        }

        &.warning {
          background: linear-gradient(135deg, #FF9800, #F57C00);
        }
      }

      .status-info {
        .status-title {
          font-size: 12px;
          opacity: 0.8;
          margin-bottom: 2px;
        }

        .status-desc {
          font-size: 14px;
          font-weight: 600;
        }
      }
    }
  }
}

// 统计卡片区域保持不变
.stats-section {
  margin-bottom: 24px;

  .stats-cards {
    .el-row {
      margin: -10px;
    }

    .el-col {
      padding: 10px;
    }
  }
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  height: 120px;
  position: relative;
  overflow: hidden;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  }

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, #1890ff, #36cbcb);
  }

  &.stat-card-1::before {
    background: linear-gradient(90deg, #ff6b6b, #ffa726);
  }

  &.stat-card-2::before {
    background: linear-gradient(90deg, #4ecdc4, #44a08d);
  }

  &.stat-card-3::before {
    background: linear-gradient(90deg, #ffa726, #ff6b6b);
  }

  &.stat-card-4::before {
    background: linear-gradient(90deg, #36cbcb, #1890ff);
  }

  .stat-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
  }

  .stat-main {
    flex: 1;

    .stat-value {
      font-size: 22px;
      font-weight: 800;
      color: #1f2d3d;
      line-height: 1;
      margin-bottom: 8px;
    }

    .stat-label {
      font-size: 14px;
      color: #5a6c7d;
      font-weight: 500;
    }
  }

  .stat-icon {
    text-align: center;

    .icon-wrapper {
      width: 50px;
      height: 50px;
      border-radius: 12px;
      background: linear-gradient(135deg, #f0f7ff, #e6f7ff);
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 20px;
      margin-bottom: 8px;
    }

    .stat-total {
      font-size: 12px;
      color: #8c98ae;
      font-weight: 500;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .dashboard {
    padding: 16px;
  }

  .announcement-card {
    padding: 20px;
    border-radius: 16px;

    .announcement-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 12px;

      .weather-info {
        align-self: stretch;
        justify-content: center;
      }
    }

    .announcement-content {
      .time-display {
        padding: 20px;

        .time-main {
          flex-direction: column;
          text-align: center;
          gap: 16px;

          .time-separator {
            width: 60px;
            height: 2px;
            margin: 0;
          }

          .date-week {
            text-align: center;
          }
        }
      }

      .system-status {
        grid-template-columns: 1fr;
      }
    }
  }

  .current-time {
    font-size: 36px !important;
  }
}

@media (max-width: 480px) {
  .announcement-header {
    .announcement-icon {
      width: 40px;
      height: 40px;

      .icon-megaphone {
        font-size: 20px;
      }
    }

    .header-content {
      .announcement-title {
        font-size: 18px;
      }
    }
  }

  .current-time {
    font-size: 32px !important;
  }
}

// 图表区域
.chart-section {
  .chart-card {
    background: white;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);

    .chart-header {
      margin-bottom: 20px;

      .chart-title {
        .main-title {
          font-size: 18px;
          font-weight: 600;
          color: #1f2d3d;
          margin-bottom: 4px;
        }

        .sub-title {
          font-size: 14px;
          color: #8c98ae;
        }
      }
    }

    .chart-container {
      .area-chart-placeholder {
        display: flex;
        height: 300px;
        //background: #fafbfc;
        border-radius: 8px;
        //border: 1px solid #e6ebf1;
        position: relative;
        overflow: hidden;
      }
    }
  }
}

</style>
